<template>
  <nav class="site-navbar" :class="'site-navbar--' + navbarLayoutType">
    <div class="site-navbar__header">
      <h1 class="site-navbar__brand" @click="$router.push({ name: 'home' })">
        <a
          class="site-navbar__brand-lg"
          href="javascript:;"
          style="font-size: 18px"
          >惠选中亿商城运营管理系统</a
        >
        <a class="site-navbar__brand-mini" href="javascript:;">惠选中亿</a>
      </h1>
    </div>
    <div class="site-navbar__body clearfix">
      <div class="flex_algin">
        <el-menu class="site-navbar__menu" mode="horizontal">
          <el-menu-item
            class="site-navbar__switch"
            index="0"
            @click="sidebarFold = !sidebarFold"
          >
            <icon-svg name="zhedie"></icon-svg>
          </el-menu-item>
          <div>{{ shopData.shop_name || '' }}</div>
        </el-menu>
      </div>
      <el-menu
        class="site-navbar__menu site-navbar__menu--right"
        mode="horizontal"
      >
        <el-menu-item class="site-navbar__avatar">
          <el-dropdown :show-timeout="0" placement="bottom">
            <span class="el-dropdown-link" style="lineHeight:53px">
              邀请二维码
            </span>
            <el-dropdown-menu slot="dropdown">
              <!-- <img class="qrcodeImg" :src="qrcode" /> -->
              <div class="qrcode qrcodeImg" ref="qrCodeUrl"></div>
            </el-dropdown-menu>
          </el-dropdown>
        </el-menu-item>
        <el-menu-item>
          <span style="color:#606266;">邀请码：{{ invitationCode }}</span>
        </el-menu-item>
        <el-menu-item index="1" @click="$router.push({ name: 'theme' })">
          <template slot="title">
            <el-badge value="new">
              <icon-svg name="shezhi" class="el-icon-setting"></icon-svg>
            </el-badge>
          </template>
        </el-menu-item>

        <el-menu-item class="site-navbar__avatar" index="3">
          <el-dropdown :show-timeout="0" placement="bottom">
            <span class="el-dropdown-link">
              <img src="~@/assets/img/avatar.png" :alt="userName" />{{
                userName
              }}
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="updatePasswordHandle()"
                >修改密码</el-dropdown-item
              >
              <el-dropdown-item @click.native="logoutHandle()"
                >退出</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-menu-item>
      </el-menu>
    </div>
    <!-- 弹窗, 修改密码 -->
    <update-password
      v-if="updatePassowrdVisible"
      ref="updatePassowrd"
    ></update-password>
  </nav>
</template>

<script>
import UpdatePassword from './main-navbar-update-password'
import QRCode from 'qrcodejs2'
import seviceApi from '@/api/serviceApi.js'
import { clearLoginInfo } from '@/utils'
export default {
  data() {
    return {
      updatePassowrdVisible: false,
      qrcode: '', // 二维码
      invitationCode: '', // 邀请码
      shopData: {}, //店铺信息
      userInfo: {}
    }
  },
  components: {
    UpdatePassword
  },
  computed: {
    navbarLayoutType: {
      get() {
        return this.$store.state.common.navbarLayoutType
      }
    },
    sidebarFold: {
      get() {
        return this.$store.state.common.sidebarFold
      },
      set(val) {
        this.$store.commit('common/updateSidebarFold', val)
      }
    },
    mainTabs: {
      get() {
        return this.$store.state.common.mainTabs
      },
      set(val) {
        this.$store.commit('common/updateMainTabs', val)
      }
    },
    userName: {
      get() {
        return this.$store.state.user.name
      }
    }
  },
  mounted() {
    // let path = {
    //   path: 'pages/index/index'
    // }
    // seviceApi.getOssQrcode(path).then(res => {
    //   if (res) {
    //     this.qrcode = res.data.data
    //     this.invitationCode = res.data.invitation_code
    //   }
    // })
    seviceApi.getInvitationCode().then(res => {
      if (res && res.data.code == 200) {
        this.invitationCode = res.data.invitation_code
        this.creatQrCode()
      }
    })
    this.userInfo = JSON.parse(localStorage.getItem('userInfo'))
    if (this.userInfo.isShop) {
      this.getShopName()
    }
  },
  methods: {
    // 生成二维码
    creatQrCode() {
      var qrcode = new QRCode(this.$refs.qrCodeUrl, {
        text:
          this.$config.H5_URL + '/pages/register?code=' + this.invitationCode, // 需要转换为二维码的内容
        width: 100,
        height: 100,
        colorDark: '#000000',
        colorLight: '#ffffff',
        correctLevel: QRCode.CorrectLevel.H
      })
    },
    // 获取店铺名称
    getShopName() {
      seviceApi.getYxtShopInfo().then(res => {
        if (res && res.data.code == 200) {
          this.shopData = res.data.data
        }
      })
    },
    // 修改密码
    updatePasswordHandle() {
      this.updatePassowrdVisible = true
      this.$nextTick(() => {
        this.$refs.updatePassowrd.init()
      })
    },
    // 退出
    logoutHandle() {
      this.$confirm(`确定进行[退出]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl('/sys/logout'),
            method: 'post',
            data: this.$http.adornData()
          }).then(({ data }) => {
            if (data && data.code === 200) {
              clearLoginInfo()
              this.$router.push({ name: 'login' })
            }
          })
        })
        .catch(() => {})
    }
  }
}
</script>
<style lang="scss" scoped>
.qrcodeImg {
  width: 146px;
  height: 146px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.flex_algin {
  ul {
    display: flex;
    align-items: center;
  }
}
</style>
